<?php
	$event_id = $_POST['event_id'];
	require_once 'constants.php';
	$con = get_connection();
	$query="SELECT * FROM `hh_seat_allocate_$event_id`";
	//$event = $fb->api('/'.$event_id.'?access_token='.$access_token, 'GET');
	if($result = mysql_query($query, $con))
	{
		while($row = mysql_fetch_array($result))
		{
			if(isset($_POST[$row['seat_name']]) && $_POST[$row['seat_name']]=='on')
				book_seat($event_id, $row['seat_no'], $user, $con);
		}
	}
	else
		die('Error in query: ' . mysql_error());
	close_connection($con);	
	$access_token = $fb->getAccessToken();
	$event = $fb->api('/'.$event_id.'?access_token='.$access_token, 'GET');
	echo "test";
	if($user != $event['owner']['id'])
	{
		$result1 = $fb->api('/'.$event_id.'/attending/','POST',array("access_token" => $access_token));
		$feed_dir = '/'.$user.'/feed/';  //to the UID you want to send to
		$message_str =  'I will be attending '.$event['name'].'!';
		$msg_body = array('access_token' => $access_token,
				'name' => $event['name'],
				'message' => $message_str,
				'description' => $event['description'],
				'caption' => $event['name'],
                'link' => 'http://localhost/hophacks/event_page.php?event_id='.$event_id,
				'actions' => array(array('name' => 'Grab a seat!',
						'link' => 'http://localhost/hophacks/event_page.php?event_id='.$event_id))
		);
		try {
			//this is the API call that does it all
			$result = $fb->api($feed_dir, 'post', $msg_body);
		}
		catch (Exception $e) {
			$err_str = $e->getMessage();
		}
	}
		header("Location: event_page.php?event_id=".$event_id);
	
	
?>